/*
package com.jfzt;

import com.alibaba.fastjson2.JSONObject;
import com.alibaba.fastjson2.TypeReference;
import com.jfzt.pojo.KeycloakTokenUser;
import com.jfzt.pojo.UserJsonVO;
import jakarta.servlet.http.HttpServletRequest;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.*;

*/
/**
 * @author: shunpeng.hu
 * @date: 2024/1/8 10:08
 *//*

public class utils {

    public static void record(HttpServletRequest request) throws IOException {
        // 获取请求的基本信息
        String method = request.getMethod(); // 获取请求方法（GET, POST等）
        String requestURI = request.getRequestURI(); // 获取请求的URI
        String queryString = request.getQueryString(); // 获取查询字符串
        String remoteAddr = request.getRemoteAddr(); // 获取客户端IP地址
        String protocol = request.getProtocol(); // 获取协议（HTTP/1.1等）


        String localAddr = request.getLocalAddr(); // 获取客户端IP地址
        String localPort = String.valueOf(request.getLocalPort()); // 获取客户端端口

        // 获取请求头信息
        Map<String, String> headers = new HashMap<>();
        Enumeration<String> headerNames = request.getHeaderNames();
        // 判断 + 遍历请求体
        while (headerNames.hasMoreElements()) {
            String headerName = headerNames.nextElement();
            headers.put(headerName, request.getHeader(headerName));
        }

        // 获取请求体内容
        Object requestBody = readRequestBody(request, UserJsonVO.class);

        // 这里可以记录到文件、数据库或控制台等
        // 为了简单起见，这里只打印到控制台
        StringBuilder sb = new StringBuilder();
        sb.append("Method: ").append(method).append("\n");
        sb.append("Request URI: ").append(requestURI).append("\n");
        sb.append("Query String: ").append(queryString).append("\n");
        sb.append("Remote Address: ").append(remoteAddr).append("\n");
        sb.append("Protocol: ").append(protocol).append("\n");
        sb.append("Headers: ").append(headers).append("\n");
        sb.append("RequestBody: ").append((UserJsonVO)requestBody).append("\n");
        sb.append("LocalAddr: ").append(localAddr).append("\n");
        sb.append("LocalPort: ").append(localPort).append("\n");


        // 输出日志信息
        System.out.println(sb.toString());
    }

    private static Object readRequestBody(HttpServletRequest request, Class<UserJsonVO> className) throws IOException {
        StringBuilder stringBuilder = new StringBuilder();
        BufferedReader bufferedReader = null;
        try {
            InputStream inputStream = request.getInputStream();
            if (inputStream != null) {
                bufferedReader = new BufferedReader(new InputStreamReader(inputStream));
                char[] charBuffer = new char[128];
                int bytesRead = -1;
                while ((bytesRead = bufferedReader.read(charBuffer)) > 0) {
                    stringBuilder.append(charBuffer, 0, bytesRead);
                }
            } else {
                stringBuilder.append("");
            }
        } finally {
            if (bufferedReader != null) {
                bufferedReader.close();
            }
        }

        //解析JSONObject请求体参数
        // 将请求体字符串解析为JSONObject
        String body = stringBuilder.toString();
        if (Objects.isNull(className)){
            System.out.println(body);
            return  JSONObject.parseObject(body);
        }
        System.out.println(body);
        return JSONObject.parseObject(body, new TypeReference<UserJsonVO>() {
        });
    }



}
*/
